package com.jjt.kiki.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jjt.kiki.bean.Ip;
public class IpDao extends BaseDao{
	
	public void delete(int id) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("delete from tb_ip where ip_id = ?");
			statement.setInt(1,id);
			statement.execute();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * jjtEatJava
	 * 2017年5月9日
	 * @param i
	 * @return
	 */
	public List<Ip> findTop(int size) {
		Connection conn = null;
		PreparedStatement statement =null;
		List<Ip> list = new ArrayList<Ip>(size);
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("select * from tb_ip  limit ?");
			statement.setInt(1,size);
			ResultSet set = statement.executeQuery();
			while(set.next()) {
				Ip ip = new Ip();
				ip.setIpHost(set.getString("ip_host"));
				ip.setIpPort(set.getInt("ip_port"));
				ip.setIpId(set.getInt("ip_id"));
				list.add(ip);
			}
			statement.close();
			conn.close();
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void save(Ip ip) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("insert into tb_ip (ip_host,ip_port) values(?,?)");
			statement.setString(1, ip.getIpHost());
			statement.setInt(2, ip.getIpPort());
			statement.execute();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			} // 操作不成功，回滚事务 
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public int countByIp(Ip ip) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("select count(*) from tb_ip where ip_host = ? and ip_port = ?");
			statement.setString(1,ip.getIpHost());
			statement.setInt(2, ip.getIpPort());
			ResultSet set = statement.executeQuery();
			set.next();
			int count = set.getInt(1);
			statement.close();
			conn.close();
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}
}
